package
{
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class ListSpatialIndex implements ISpatialIndex
	{
		private var m_objects:Vector.<ISpatialIndexable>;
		public function ListSpatialIndex()
		{
			m_objects = new Vector.<ISpatialIndexable>;
		}
		
		
		public function add(entity:ISpatialIndexable):void{
			m_objects.push(entity);
		}
		
		public function remove(entity:ISpatialIndexable):void{
			m_objects.splice(m_objects.indexOf(entity),1);
		}
		
		public function find(entity:ISpatialIndexable):Boolean{
			return (m_objects.indexOf(entity) != -1);
		}
		
		public function getPointCollisions(pnt:Point):Vector.<ISpatialIndexable>{
			var collisions:Vector.<ISpatialIndexable> = new Vector.<ISpatialIndexable>;
			for each(var obj:ISpatialIndexable in m_objects){
				if(obj.getRect().containsPoint(pnt)){
					collisions.push(obj);
				}
			}
			return collisions;
		}
		
		public function getRectCollisions(rect:Rectangle):Vector.<ISpatialIndexable>{
			var collisions:Vector.<ISpatialIndexable> = new Vector.<ISpatialIndexable>;
			for each(var obj:ISpatialIndexable in m_objects){
				if(obj.getRect().intersects(rect)){
					collisions.push(obj);
				}
			}			
			return collisions;			
		}
	}
}